package applet.admin.components.conference;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.BevelBorder;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;

import data.CMSRes;

import applet.misc.action.EnableActionListener;
import applet.misc.document.EnableDocumentListener;
import applet.misc.graphics.AdminBackgroundPanel;

public class CMSAdminConferenceView extends AdminBackgroundPanel
{
	private static final long serialVersionUID = -1977788206614724569L;

	private JTextField nameField;
	private JTextArea descriptionField;
	
	private JComboBox locationBox;
	private DefaultComboBoxModel locationModel;
	private JButton newLocButton;
	private JButton delLocButton;
	
	private JButton saveButton;
	private JButton resetButton;
	
	public CMSAdminConferenceView()
	{
		super(new GridBagLayout());
		setBorder(new EmptyBorder(25, 25, 25, 25));

		defineComponents();
		JPanel conferenceDialog = new JPanel(new BorderLayout());
		conferenceDialog.setBorder(new BevelBorder(BevelBorder.RAISED));
		
				JPanel titlePane = new JPanel(new BorderLayout());
				titlePane.setBackground(CMSRes.Colors.get("Admin Color Medium"));
				titlePane.setBorder(new EmptyBorder(10, 10, 10, 10));
					JLabel title = new JLabel("Conference Info:");
					title.setFont(title.getFont().deriveFont(Font.BOLD));
					titlePane.add(title, BorderLayout.WEST);
				conferenceDialog.add(titlePane, BorderLayout.NORTH);
				
			conferenceDialog.add(getContentPanel(), BorderLayout.CENTER);
			conferenceDialog.add(getSaveBar(), BorderLayout.SOUTH);
		add(conferenceDialog);
	}
	
	public void defineComponents()
	{
		saveButton = new JButton("Save");
		saveButton.setFont(saveButton.getFont().deriveFont(Font.BOLD));
		saveButton.setPreferredSize(new Dimension(200, 30));
		saveButton.addActionListener(new SaveListener());
		saveButton.addActionListener(new EnableActionListener(saveButton, false));
		
		resetButton = new JButton("Reset");
		resetButton.setFont(resetButton.getFont().deriveFont(Font.BOLD));
		resetButton.setPreferredSize(new Dimension(200, 30));
		resetButton.addActionListener(new ResetListener());
		resetButton.addActionListener(new EnableActionListener(saveButton, false));
		resetButton.addActionListener(new EnableActionListener(resetButton, false));
		saveButton.addActionListener(new EnableActionListener(resetButton, false));
		
		nameField = new JTextField(CMSRes.conference().name());
		nameField.setPreferredSize(new Dimension(150, 30));
		nameField.getDocument().addDocumentListener(new EnableDocumentListener(resetButton, true));
		nameField.getDocument().addDocumentListener(new EnableDocumentListener(saveButton, true));
		
		descriptionField = new JTextArea(CMSRes.conference().description());
		descriptionField.getDocument().addDocumentListener(new EnableDocumentListener(resetButton, true));
		descriptionField.getDocument().addDocumentListener(new EnableDocumentListener(saveButton, true));
		descriptionField.setWrapStyleWord(true);
		descriptionField.setLineWrap(true);

		/*locationModel = new DefaultListModel();
		for(String location : CMSRes.conference().locations())
			locationModel.addElement(location);
		locationList = new JList(locationModel);*/
		
		locationModel = new DefaultComboBoxModel();
		for(String location : CMSRes.conference().locations())
			locationModel.addElement(location);
		locationBox = new JComboBox(locationModel);
		locationBox.setPreferredSize(new Dimension(150, 30));
		
		newLocButton = new JButton("Add Event Location");
		newLocButton.setPreferredSize(new Dimension(150, 30));
		newLocButton.addActionListener(new NewLocationListener());
		newLocButton.addActionListener(new EnableActionListener(saveButton, true));
		newLocButton.addActionListener(new EnableActionListener(resetButton, true));
		
		delLocButton = new JButton("Delete Event Location");
		delLocButton.setPreferredSize(new Dimension(150, 30));
		delLocButton.addActionListener(new DeleteLocationListener());
		delLocButton.addActionListener(new EnableActionListener(saveButton, true));
		delLocButton.addActionListener(new EnableActionListener(resetButton, true));
	}
	
	private JPanel getContentPanel()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new CompoundBorder(new BevelBorder(BevelBorder.LOWERED), new EmptyBorder(5, 5, 5, 5)));
		
		panel.add(getNamePanel(), BorderLayout.NORTH);
		panel.add(getLocationsPanel(), BorderLayout.CENTER);
		panel.add(getDescriptionPanel(), BorderLayout.SOUTH);
		
		return panel;
	}
	
	private JPanel getNamePanel()
	{
		JPanel panel = new JPanel(new GridLayout(1, 2));
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 3, 10, 10));
				JLabel labelHelper = new JLabel("Conference Name:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.EAST);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(10, 3, 10, 10));
			layoutHelper.add(nameField, BorderLayout.WEST);
		panel.add(layoutHelper);
		return panel;
	}
	
	private JPanel getLocationsPanel()
	{
		JPanel panel = new JPanel(new GridLayout(1, 2));
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
			JPanel layoutHelper = new JPanel(new BorderLayout());
				JPanel layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 3, 10, 10));
					JLabel labelHelper = new JLabel("Event Locations:");
					labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layout2.add(labelHelper, BorderLayout.EAST);
			layoutHelper.add(layout2, BorderLayout.NORTH);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 3, 10, 10));
					JPanel layout3 = new JPanel(new BorderLayout());
					layout3.add(locationBox, BorderLayout.WEST);
				layout2.add(locationBox, BorderLayout.NORTH);
					layout3 = new JPanel(new BorderLayout());
					layout3.setBorder(new EmptyBorder(10, 0, 10, 0));
					layout3.add(newLocButton, BorderLayout.WEST);
				layout2.add(layout3, BorderLayout.CENTER);
					layout3 = new JPanel(new BorderLayout());
					layout3.add(delLocButton, BorderLayout.WEST);
				layout2.add(layout3, BorderLayout.SOUTH);
			layoutHelper.add(layout2);
		panel.add(layoutHelper);
		return panel;
	}
	
	private JPanel getDescriptionPanel()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
		
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(5, 5, 5, 5));
				JLabel labelHelper = new JLabel("Description:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper, BorderLayout.NORTH);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setPreferredSize(new Dimension(150, 150));
			layoutHelper.setBorder(new EmptyBorder(5, 5, 5, 5));
			layoutHelper.add(new JScrollPane(descriptionField), BorderLayout.CENTER);
		panel.add(layoutHelper, BorderLayout.CENTER);
		
		return panel;
	}
	
	private JPanel getSaveBar()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBackground(CMSRes.Colors.get("Admin Color Medium"));
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
		panel.add(saveButton, BorderLayout.EAST);
		panel.add(resetButton, BorderLayout.WEST);
		return panel;
	}
	
	private class NewLocationListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			String location = JOptionPane.showInputDialog(CMSRes.frame(),
					  "Enter new location:",
					  "New Location",
					  JOptionPane.QUESTION_MESSAGE);
			locationModel.addElement(location);
			System.out.println(location);
		} }
	
	private class DeleteLocationListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			int index = locationBox.getSelectedIndex();
			if(index != -1 && index < locationModel.getSize())
				locationModel.removeElementAt(index);
		} }
	
	private class SaveListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			CMSRes.conference().setName(nameField.getText());
			CMSRes.conference().setDescription(descriptionField.getText());
			CMSRes.conference().locations().clear();
			for(int i = 0; i < locationModel.getSize(); i++)
				CMSRes.conference().locations().add((String) locationModel.getElementAt(i));
			CMSRes.conference().saveData();
		} }
	

	private class ResetListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			nameField.setText(CMSRes.conference().name());
			descriptionField.setText(CMSRes.conference().description());
			locationModel.removeAllElements();
			for(String location : CMSRes.conference().locations())
				locationModel.addElement(location);
		}
	}
}